package com.zhang.controller;

import com.zhang.application.StaffingApplication;
import com.zhang.dao.DepartmentDao;
import com.zhang.dao.EmployeeDao;
import com.zhang.pojo.Department;
import com.zhang.pojo.Employee;
import com.zhang.service.DepartmentService;
import com.zhang.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ZhangJunqi
 * @Date 2022/2/23 -20:15
 */
@Controller
public class EmployeeController {
    @Resource
    private EmployeeService employeeService;
    @Resource EmployeeDao employeeDao;
    @Resource DepartmentDao departmentDao;
    @GetMapping(value = "/getAllEmployee")
    public String getAllEmployee(Model model){
        List<Employee> employeeList = null;
        if(StaffingApplication.employeeMap.size ()<=0){
            employeeList=employeeService.getAllEmployee ();
            if(employeeList!=null&&employeeList.size ()>0){
                for (Employee employee:employeeList){
                    StaffingApplication.employeeMap.put (employee.getId (),employee);
                }

            }
        }else {
            //或者一个一个Map导List 这么弄id不是按顺序的
            employeeList=new ArrayList<> (StaffingApplication.employeeMap.values ());
        }
        model.addAttribute ("employeeList",employeeList);
        return "emp/list";
    }

    @GetMapping(value = "/emp")
    public String addEmpPage(Model model){
        List<Department> departmentList=StaffingApplication.departmentList;
        model.addAttribute ("departmentList",departmentList);
        return "emp/addEmp";
    }

    @PostMapping(value = "/emp")
    public String addEmp(Employee employee){
        int res=employeeService.saveEmployee (employee);
        if (res>0){
            System.out.println("添加成功"+employee.toString ());
            StaffingApplication.employeeMap.put (employee.getId (),employee);
        }
        return "redirect:getAllEmployee";
    }

    @GetMapping(value = "/empUpdate/{id}")
    public String updateEmpPage(@PathVariable("id") Integer id,Model model){
        Employee employee=StaffingApplication.employeeMap.get (id);
        model.addAttribute ("employee",employee);
        List<Department> departmentList=StaffingApplication.departmentList;
        model.addAttribute ("departmentList",departmentList);
        return "emp/updateEmp";
    }
    @GetMapping(value = "/empDelete/{id}")
    public String empDelete(@PathVariable("id") Integer id){
        System.out.println(id);
        int res=employeeService.deleteEmployeeById(id);
        if (res>0){
            System.out.println("删除成功"+id);
            StaffingApplication.employeeMap.remove (id);
        }
        return "redirect:getAllEmployee";
    }

    @PostMapping(value = "/empUpdate")
    public String empUpdate(Employee employee){
        System.out.println(employee);
        int res=employeeService.empUpdate(employee);
        if (res>0){
            System.out.println("修改成功"+employee.toString ());
            StaffingApplication.employeeMap.put (employee.getId (),employee);
        }
        return "redirect:getAllEmployee";
    }
}
